package com.zt.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.org.rapid_framework.beanutils.BeanUtils;

import com.zt.utils.BaseException;
import com.zt.utils.ControllerResult;
import com.zt.utils.ConvertRegisterHelper;
import com.zt.utils.DataExceptionProcess;

/**
 * BaseController
 * 
 * @author zhangzhw
 * @describe 基本控制器类
 */
public class BaseController extends MultiActionController {

	static {
		// 注册converters
		ConvertRegisterHelper.registerConverters();
	}

	public void copyProperties(Object target, Object source) {
		BeanUtils.copyProperties(target, source);
	}

	public <T> T copyProperties(Class<T> destClass, Object orig) {
		return BeanUtils.copyProperties(destClass, orig);
	}

	// 以下为异常处理方法集

	@ExceptionHandler(DataAccessException.class)
	public ModelAndView resolveDataAccessException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		logger.info("DataAccessException Catched!");
		logger.info(ex.getMessage());
		Map<String, Object> map = new HashMap<String, Object>();

		ControllerResult cs = DataExceptionProcess
				.processDBAException((DataAccessException) ex);
		map.put("success", false);
		map.put("title", cs.getTitle());
		map.put("errorMsg", cs.getErrorMessage());
		map.put("detailMessage", cs.getDetailMessage());

		return new ModelAndView("/sys/exception", "map", map);

	}

	/**
	 * resolveBaseException
	 * 
	 * @param request
	 * @param response
	 * @param handler
	 * @param ex
	 * @return 处理自定义异常
	 */
	@ExceptionHandler(BaseException.class)
	public ModelAndView resolveBaseException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		logger.info(ex.getMessage());
		BaseException be = (BaseException) ex;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("title", be.getExceptionTitle());
		map.put("errorMsg", be.getExceptionCode());
		map.put("detailMessage", be.getExceptionDetail());
		return new ModelAndView("/sys/exception", "map", map);

	}

	/**
	 * resolveIOException
	 * 
	 * @describe 处理IO异常
	 * 
	 */
	@ExceptionHandler(IOException.class)
	public ModelAndView resolveIOException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		logger.info(ex.getMessage());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("title", "系统I/O异常");
		map.put("errorMsg", "发生未知系统I/O异常");
		map.put("detailMessage", ex.getMessage());
		return new ModelAndView("/sys/exception", "map", map);

	}

	/**
	 * resolveException
	 * 
	 * @describe 处理未知异常
	 */
	@ExceptionHandler(Exception.class)
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		logger.info(ex.getMessage());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("title", "未知异常");
		map.put("errorMsg", "发生未知异常");
		map.put("detailMessage", ex.getMessage());
		return new ModelAndView("/sys/exception", "map", map);

	}

}
